﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameLibrary;

namespace UserInterface.Helpers
{
    public class RelationshipInfomation
    {
        public string[,] matrix { get; private set; }

        public RelationshipInfomation(Dictionary<uint, Dictionary<uint,DiplomaticRelationshipType>> relationships)
        {
            this.ChangeIntoMatrix(relationships);         
        }

        private void ChangeIntoMatrix(Dictionary<uint, Dictionary<uint, DiplomaticRelationshipType>> rel)
        {
            initialArray( rel.Keys.Count+1, rel.Keys.Count+1);

            List<uint> players = new List<uint>(rel.Keys);

            InsertPlayers(players);


            for (int i = 1; i < players.Count+1; i++)
            {
                var currentPlayer = players[i - 1];
                var currentPlayerRelations = rel[players[i - 1]];
                InsertRelations(players,
                                currentPlayer,
                                currentPlayerRelations);
            }
        }
        private void initialArray(int x, int y)
        {
            this.matrix = new string[x,y];
            for (int i = 0; i < x; i++)
                for (int j = 0; j < y; j++)
                    this.matrix[i, j] = string.Empty;
        }

        private void InsertPlayers(List<uint> players)
        {
            for (int i = 1; i < this.matrix.GetLength(0); i++)
            {
                this.matrix[i, 0] = players[i-1].ToString();
                this.matrix[0, i] = players[i-1].ToString();
            }
        }

        private void InsertRelations(List<uint> players, uint currentPlayer,
                                    Dictionary<uint, DiplomaticRelationshipType> relations)
        {
            int currentPlayerPosition = players.IndexOf(currentPlayer);

            foreach (var r in relations)
            {
                int index = players.IndexOf(r.Key);
                this.matrix[currentPlayerPosition+1, index+1] = r.Value.ToString();
            }
        }
    }
}
